﻿using SchoolManagementSystem.Classes;
using SchoolManagementSystem.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace SchoolManagementSystem.Forms
{
    public partial class InsertForm : Form
    {
        private ParentForm parentForm;
        private SQLiteConnecter sqlConnecter;
        private string[] tables = { "schools", "classes", "students" };
        private int tableChosen = 0;
        private List<KeyValuePair<string, System.Windows.Forms.TextBox>> listTextBoxSchool;
        private List<KeyValuePair<string, System.Windows.Forms.TextBox>> listTextBoxClass;
        private List<KeyValuePair<string, System.Windows.Forms.TextBox>> listTextBoxStudent;

        public InsertForm(ParentForm parentForm,SQLiteConnecter connecter)
        {
            InitializeComponent();
            this.parentForm = parentForm;
            this.MdiParent = parentForm;
            this.Dock = DockStyle.Fill;
            this.sqlConnecter = connecter;
            textBoxInit();
        }

        /// <summary>
        /// 点击添加按钮，打包窗体的相关数据，
        /// 并调用SqliteConnecter的方法向数据库添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonInsert_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<string, string>> key_value=new List<KeyValuePair<string, string>>();
            List<KeyValuePair<string, System.Windows.Forms.TextBox>> getList;
            switch (tableChosen)
            {
                case 0: getList = listTextBoxSchool; break;
                case 1: getList = listTextBoxClass; break;
                case 2: 
                default:getList = listTextBoxStudent; break;
            }
            foreach(var item in getList)
            {
                key_value.Add(new KeyValuePair<string, string>(item.Key, item.Value.Text));
            }
            try
            {
                sqlConnecter.insert(tables[tableChosen], key_value);
                textBoxInsertInfo.Text += "添加数据成功，数据表：" + tables[tableChosen] +"\r\n";
            }
            catch(ConnectFailedException exp)
            {
                textBoxInsertInfo.Text += exp.ToString()+"\r\n";
            }
            catch(LoggerFailedException exp)
            {
                textBoxInsertInfo.Text += exp.ToString()+"\r\n";
            }
        }

        /// <summary>
        /// 选择操作对象（学校、班级、学生），并更新窗体状态和控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Name.Equals("radioButtonSchool"))
            {
                this.panelSchoolInsert.Show();
                this.panelClassInsert.Hide();
                this.panelStudentInsert.Hide();
                tableChosen = 0;
            }else if (((RadioButton)sender).Name.Equals("radioButtonClass"))
            {
                this.panelSchoolInsert.Hide();
                this.panelClassInsert.Show();
                this.panelStudentInsert.Hide();
                tableChosen = 1;
            }
            else if (((RadioButton)sender).Name.Equals("radioButtonStudent"))
            {
                this.panelSchoolInsert.Hide();
                this.panelClassInsert.Hide();
                this.panelStudentInsert.Show();
                tableChosen = 2;
            }
        }

        /// <summary>
        /// 重置按钮，将所有信息输入框清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonReset_Click(object sender, EventArgs e)
        {
            foreach (var item in listTextBoxClass)
            {
                item.Value.Clear();
            }
            foreach (var item in listTextBoxSchool)
            {
                item.Value.Clear();
            }
            foreach (var item in listTextBoxStudent)
            {
                item.Value.Clear();
            }
        }

        /// <summary>
        /// 对输入属性的文本框按照不同的数据表进行分组
        /// </summary>
        private void textBoxInit()
        {

            this.listTextBoxSchool = new List<KeyValuePair<string, System.Windows.Forms.TextBox>>();
            this.listTextBoxClass = new List<KeyValuePair<string, System.Windows.Forms.TextBox>>();
            this.listTextBoxStudent = new List<KeyValuePair<string, System.Windows.Forms.TextBox>>();
            this.listTextBoxSchool.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("address", textBoxAddress));
            this.listTextBoxSchool.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("build_time", textBoxBuildTime));
            this.listTextBoxClass.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("class", textBoxClass_class));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("class", textBoxClass_stu));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("gender", textBoxGender));
            this.listTextBoxClass.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("grade", textBoxGrade_class));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("grade", textBoxGrade_stu));
            this.listTextBoxSchool.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("headmaster", textBoxHeadMaster));
            this.listTextBoxClass.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("majority", textBoxMajority_class));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("majority", textBoxMajority_stu));
            this.listTextBoxSchool.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("school_id", textBoxSchoolId));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("school_name", textBoxSchoolName_stu));
            this.listTextBoxClass.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("school_name", textBoxSchoolName_class));
            this.listTextBoxSchool.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("school_name", textBoxSchoolName));
            this.listTextBoxSchool.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("school_phone", textBoxSchoolPhone));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("stu_id", textBoxStu_id));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("stu_name", textBoxStu_name));
            this.listTextBoxStudent.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("stu_phone", textBoxStu_phone));
            this.listTextBoxClass.Add(new KeyValuePair<string, System.Windows.Forms.TextBox>("tutor", textBoxTutor));
        }
    }
}
